<template>
  <div class="home_container">
    <!-- 框架 -->
    <el-container>
      <!-- 头部 -->
      <el-header>
        <div class="header_lef">
          <img src="../assets/heima.png" alt="" />
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="loginback">退出</el-button></el-header
      >
      <!-- 内容 -->
      <el-container>
        <!-- 左侧 -->
        <el-aside :width="iscollapse ? '64px' : '200px'">
          <div class="toggle_menu" @click="iscollapses">|||</div>
          <!-- 导航菜单 -->
          <el-menu
            :default-active="rout"
            class="el-menu-vertical-demo"
            background-color="#313744"
            text-color="#fff"
            active-text-color="#2a72d8"
            unique-opened
            :collapse="iscollapse"
            :collapse-transition="false"
            router
          >
            <!-- 一级导航 -->
            <el-submenu
              :index="item.id + ''"
              v-for="item in list"
              :key="item.id"
            >
              <!-- 模板 -->
              <template slot="title">
                <!-- 图标 -->
                <i :class="icons[item.id]"></i>
                <!-- 内容 -->
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级导航 -->
              <el-menu-item
                :index="'/' + menuitem.path"
                v-for="menuitem in item.children"
                :key="menuitem.id"
                @click="defaultactive('/' + menuitem.path)"
              >
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ menuitem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 右侧 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      icons: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      iscollapse: false,
      // 用来存储路由
      rout: ''
    }
  },
  created() {
    this.getshow()
    this.rout = window.sessionStorage.getItem('default')
  },
  methods: {
    loginback() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getshow() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) this.$message.error('res.meta.msg')
      this.list = res.data
      // console.log(res)
    },
    iscollapses() {
      this.iscollapse = !this.iscollapse
    },
    defaultactive(act) {
      this.rout = act
      window.sessionStorage.setItem('default', this.rout)
    }
  }
}
</script>

<style lang="less" scoped>
.el-header {
  background-color: #363d40;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  .header_lef {
    display: flex;
    align-items: center;
    span {
      margin-left: 10px;
      color: #e9edf1;
      font-size: 16px;
    }
  }
}
.el-aside {
  background-color: #313744;
  .iconfont {
    margin-right: 5px;
  }
  .el-menu {
    border-right: 0;
  }
}
.el-main {
  background-color: #e9edf1;
}
.home_container {
  height: 100%;
  .el-container {
    height: 100%;
  }
}
.toggle_menu {
  background-color: #484f62;
  line-height: 30px;
  text-align: center;
  color: #e9edf1;
}
</style>